<?php

namespace app\cps\service;

use app\model\ResponsibilityPerson;
use think\facade\Session;
use app\model\Company;
use app\model\OfficialAccounts;
use app\common\Error;
use think\facade\Cache;
use app\common\Common;

class LoginService
{
    private $model;
    private $companyModel;
    private $officialAccountsModel;

    public function __construct(ResponsibilityPerson $model, Company $companyModel, OfficialAccounts $officialAccountsModel)
    {
        $this->model = $model;
        $this->companyModel = $companyModel;
        $this->officialAccountsModel = $officialAccountsModel;
    }

    // cps用户登录
    public function cpsUserLogin($params)
    {
        $username = addslashes(strip_tags($params['username']));
        $password = addslashes(strip_tags($params['password']));

        $data = $this->model->getInfoByWhere(['name' => $username], 'id, name, password, official_account_id, company_id, used_official_account_id, is_distributor, pid, is_branch_office_manage,is_depart');

        if (empty($data)) {
            return ['code' => Error::NO_CHARGE_REQUIRED_CODE, 'message' => Error::USER_NOT_EXIXTS];
        }

        if (!password_verify($password, $data['password'])) {
            return ['code' => Error::NO_CHARGE_REQUIRED_CODE, 'message' => Error::PASSWORD_ERROR];
        }

        if ($data['is_depart']==1){
            return ['code' => Error::NO_CHARGE_REQUIRED_CODE, 'message' => Error::IS_DEPAET_ERROR];
        }

        $token = Common::createToken($data['id']);
        $data['token'] = $token;

        $redis = Cache::store('redis')->handler();
        $redis->select(3);

        $officialAccountIds = explode(',', $data['official_account_id']);

        $officialAccountsData = $this->officialAccountsModel->getDataByWhere(['id' => $officialAccountIds], 'id, name, is_default, app_id');

        foreach ($officialAccountsData as $key => $item) {
            if ($item['id'] == $data['used_official_account_id']) {
                $officialAccountsData[$key]['used_official_account_id'] = 1;
            } elseif (empty($data['used_official_account_id']) && $item['is_default'] == 1) {
                $officialAccountsData[$key]['used_official_account_id'] = 1;
            } else {
                $officialAccountsData[$key]['used_official_account_id'] = 0;
            }

            if (empty($data['used_official_account_id']) && $item['is_default'] == 1) {
                $usedOfficialAccountId = $item['id'];
            }
        }

        if (empty($usedOfficialAccountId)) {
            $usedOfficialAccountId = $officialAccountIds[0];
        }

        $data['official_accounts_data'] = $officialAccountsData;

        if (empty($data['used_official_account_id'])) {
            $this->model->edit(['id' => $data['id'], 'used_official_account_id' => $usedOfficialAccountId, 'updated_at' => time()]);
        } else {
            $usedOfficialAccountId = $data['used_official_account_id'];
        }

        $redis->set($token, json_encode([
            'token' => $token,
            'cps_user_id' => $data['id'],
            'cps_user_name' => $data['name'],
            'cps_used_official_account_id' => $usedOfficialAccountId,
            'cps_official_accounts_data' => $officialAccountsData,
            'is_branch_office_manage' => $data['is_branch_office_manage'],
            'is_distributor' => $data['is_distributor'],
            'pid' => $data['pid']
        ]), config('redis_key.cps_access_token_expire'));

        unset($data['password']);

        $data['cps_used_official_account_id'] = $usedOfficialAccountId;

        // 添加登录次数
        $this->model->where(['id' => $data['id']])->inc('frequency')->update();
        return $data;
    }
}